/**
 * Copyright (c) 2008, George Taylor
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are permitted 
 * provided that the following conditions are met:
 * 
 *   * Redistributions of source code must retain the above copyright notice, this list of 
 *     conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright notice, this list of 
 *     conditions and the following disclaimer in the documentation and/or other materials 
 *     provided with the distribution.
 *   * Neither the name of the copyright holder's organization nor the names of its contributors 
 *     may be used to endorse or promote products derived from this software without specific 
 *     prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package uk.co.taylorconsulting.annoweb.web.builder.data;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

import uk.co.taylorconsulting.annoweb.annotation.action.Action;
import uk.co.taylorconsulting.annoweb.annotation.security.Role;
import uk.co.taylorconsulting.annoweb.annotation.web.DefaultForward;
import uk.co.taylorconsulting.annoweb.annotation.web.Forward;
import uk.co.taylorconsulting.annoweb.annotation.web.QuickLink;

public class ActionHolder {
	private Action                   action;
	private Method                   method;
	private ArrayList<Role>          roles    = new ArrayList<Role>();
	private DefaultForward           defaultForward;
	private HashMap<String, Forward> forwards = new HashMap<String, Forward>();
	private Site                     site;
	private QuickLink                quickLinks;
	
	public ActionHolder(Action action, Method method, Site site) {
		this.action = action;
		this.method = method;
		this.site = site;
	}
	
	public Action getAction() {
		return action;
	}
	
	public Method getMethod() {
		return method;
	}
	
	public void addRole(Role role) {
		roles.add(role);
	}
	
	public void setDefaultForward(DefaultForward forward) {
		this.defaultForward = forward;
	}
	
	public void addForward(Forward forward) {
		if (!forwards.containsKey(forward.name())) {
			forwards.put(forward.name(), forward);
		}
	}
	
	public String toString() {
		StringBuffer str = new StringBuffer();
		str.append("  action:").append(action.value()).append("\n");
		str.append("  method:").append(method.getDeclaringClass().getCanonicalName()).append(":").append(method.getName()).append("\n");
		str.append("  Roles:\n");
		for (Role r : roles) {
			str.append("  ").append(r.value()).append(", realm=").append(r.realm()).append("\n");
		}
		str.append("  Default Forward:\n");
		if (defaultForward == null) {
			str.append("none\n");
		} else {
			str.append(defaultForward.value()).append("\n");
		}
		str.append("  Forwards:\n");
		for (Forward f : forwards.values()) {
			str.append("    ").append(f.name()).append(", path=").append(f.path()).append(", web=").append(f.web().getCanonicalName()).append(", method=").append(f.method()).append(", redirect=").append(f.redirect()).append("\n");
		}
		return str.toString();
	}
	
	public Forward getForward(String name) {
		return forwards.get(name);
	}
	
	public Collection<Forward> getForwards() {
		return forwards.values();
	}
	
	public DefaultForward getDefaultForward() {
		return defaultForward;
	}
	
	public Site getSite() {
		return site;
	}
	
	public void setSite(Site site) {
		this.site = site;
	}
	
	public ArrayList<Role> getRoles() {
		return roles;
	}
	
	public QuickLink getQuickLinks() {
		return quickLinks;
	}
	
	public void setQuickLinks(QuickLink quickLinks) {
		this.quickLinks = quickLinks;
	}
}
